﻿using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using Vision.VHelper;
using VisionDesigner;
using VisionDesigner.HPFeaturePatMatch;
using UserControl = System.Windows.Controls.UserControl;

namespace Vision.VControl
{
    /// <summary>
    /// DisplayCt.xaml 的交互逻辑
    /// </summary>
    public partial class DisplayCt : UserControl
    {
        #region Private Filed
        // 海康显示控件
        public MVDRenderActivex mVDRender = new MVDRenderActivex();
        // 输入图像
        private CMvdImage _InputImage;
        public CMvdImage InputData
        {
            get => _InputImage;
            set
            {
                _InputImage = value;
                OnInputDataChanged();
            }
        }
        // 轮廓点放大倍数
        public Int32 _Scale = 3;
        // 图片正常加载
        bool _ImageLoaded = false;
        // 枚举用于指示当前活动数据类型
        public enum ROIDataTypeEnum
        {
            RegionList,
            ROI
        }
        private CMvdPointSetF _drawFixPoint = new CMvdPointSetF();                              // 基准点
        private List<CPatMatchRegion> _drawRegionList = new List<CPatMatchRegion>();            // 绘制Region区域
        private CMvdShape _roi = null;                                                          // ROI图形
        private ROIDataTypeEnum _roiDataType;                                                   // ROI数据
        public ROIDataTypeEnum ROIDataType
        {
            get => _roiDataType;
            set
            {
                _roiDataType = value;
            }
        }
        public object? ROIData
        {
            get
            {
                return _roiDataType switch
                {
                    ROIDataTypeEnum.RegionList => _drawRegionList,
                    ROIDataTypeEnum.ROI => _roi,
                    _ => null
                };
            }
            set
            {
                switch (_roiDataType)
                {
                    case ROIDataTypeEnum.RegionList:
                        _drawRegionList = value as List<CPatMatchRegion>;
                        _roi = null;
                        break;
                    case ROIDataTypeEnum.ROI:
                        _roi = value as CMvdShape;
                        _drawRegionList = null;
                        break;
                }
            }
        }

        public List<CMvdShape> _MaskShapeList = new List<CMvdShape>();                          // 屏蔽区图形列表
        private CMvdAnnularSectorF _DrawArc = null;                                             // 绘制圆弧
        public CMvdAnnularSectorF DrawArc
        {
            get => _DrawArc;
            set
            {
                _DrawArc = value;
                OnDrawArcChanged();
            }
        }

        private bool _isArcVisible = true;
        private bool _isRectangleFVisible = true;
        private bool _areOutlinesVisible = false;
        private bool _areMatchOutlineVisible = true;
        private bool _isFixPointVisible = true;
        public List<CMvdLineSegmentF> _DrawOutlineList = new List<CMvdLineSegmentF>();          // 绘制轮廓
        public List<CMvdRectangleF> _DrawRectangleFList = new List<CMvdRectangleF>();           // 绘制矩形框
        private List<CMvdPointSetF> _MatchOutlineList = new List<CMvdPointSetF>();              // 匹配轮廓列表
        private HashSet<CMvdShape> _addedShapes = new HashSet<CMvdShape>();                     // 已经绘制添加的图形

        public ObservableCollection<ButtonViewModel> AllButtons { get; }                        // 结果显示控制按钮（支持的所有类型）
        public ObservableCollection<ButtonViewModel> VisibleButtons { get; set; }               // 结果显示控制按钮（当前支持的类型）
        public List<ROIType> SupportedROIs { get; set; } = new List<ROIType>();                 // 支持的ROI类型
        private ROIType? _selectedROI = null;                                                   // 当前选择的ROI
        public ROIType? SelectedROI
        {
            get => _selectedROI;
            set
            {
                _selectedROI = value;
                OnSelectedROIChanged();
            }
        }

        public List<ROIType> SupportedMaskROIs { get; set; } = new List<ROIType>();                 // 支持的掩膜ROI类型
        private ROIType? _selectedMaskROI = null;                                                   // 当前选择的掩膜ROI
        public ROIType? SelectedMaskROI
        {
            get => _selectedMaskROI;
            set
            {
                _selectedMaskROI = value;
                OnSelectedMaskROIChanged();
            }
        }

        #endregion

        #region Function
        public DisplayCt()
        {
            InitializeComponent();
            InitMVDRender();
            UpdateROIButtons();

            // 初始化按钮数据
            AllButtons = new ObservableCollection<ButtonViewModel>
            {
                new ButtonViewModel { Name = "圆弧", IsChecked = _isArcVisible },
                new ButtonViewModel { Name = "卡尺", IsChecked = _isRectangleFVisible },
                new ButtonViewModel { Name = "轮廓", IsChecked = _areOutlinesVisible },
                new ButtonViewModel { Name = "匹配框", IsChecked = _isRectangleFVisible },
                new ButtonViewModel { Name = "匹配轮廓", IsChecked = _areMatchOutlineVisible },
                new ButtonViewModel { Name = "基准点", IsChecked = _isFixPointVisible }
                // 添加其他按钮的逻辑...
            };
            foreach (var button in AllButtons)
            {
                button.PropertyChanged += Button_PropertyChanged;
            }
            VisibleButtons = new ObservableCollection<ButtonViewModel>();
            DataContext = this;
        }

        /// <summary>
        /// ROI RadioButton触发事件
        /// </summary>
        private void OnSelectedROIChanged()
        {
            foreach (var child in ROIPanel.Children)
            {
                if (child is RadioButton radioButton && radioButton.Tag is ROIType roiType)
                {
                    radioButton.IsChecked = roiType == SelectedROI;
                }
            }
            if (SelectedROI != null)
            {
                foreach (var child in MaskROIPanel.Children)
                {
                    if (child is RadioButton radioButton && radioButton.Tag is ROIType roiType)
                    {
                        radioButton.IsChecked = false;
                    }
                }

                // Set SelectedMaskROI to null to indicate no selection
                SelectedMaskROI = null;
            }
        }

        /// <summary>
        /// MaskROI RadioButton触发事件
        /// </summary>
        private void OnSelectedMaskROIChanged()
        {
            foreach (var child in MaskROIPanel.Children)
            {
                if (child is RadioButton radioButton && radioButton.Tag is ROIType roiType)
                {
                    radioButton.IsChecked = roiType == SelectedMaskROI;
                }
            }
            if (SelectedMaskROI != null)
            {
                foreach (var child in ROIPanel.Children)
                {
                    if (child is RadioButton radioButton && radioButton.Tag is ROIType roiType)
                    {
                        radioButton.IsChecked = false;
                    }
                }

                // Set SelectedROI to null to indicate no selection
                SelectedROI = null;
            }
        }

        public void UpdateVisibleButtons(List<string> buttonNames)
        {
            var buttonsToShow = AllButtons.Where(b => buttonNames.Contains(b.Name)).ToList();
            VisibleButtons.Clear();
            foreach (var button in buttonsToShow)
            {
                VisibleButtons.Add(button);
            }
        }

        private void InitMVDRender()
        {
            mVDRender = _mVDRender.mVDRender;
            mVDRender.SetConfiguration((uint)MVD_RENDER_PARAM_KEY.MvdRenderInteractType, (int)MVDRenderInteractType.StandardAndCustom);
            mVDRender.MVDMouseEvent += MVDRender_MVDMouseEvent;
            mVDRender.MVDShapeChangedEvent += MVDRender_MVDShapeChangedEvent;
            ROIDataType = ROIDataTypeEnum.ROI; // 默认使用ROI
        }

        private void OnInputDataChanged()
        {
            // 在这里添加你的初始化逻辑
            // 例如，初始化工具，处理图像等
            if (_InputImage != null)
            {
                if (MVD_PIXEL_FORMAT.MVD_PIXEL_RGB_RGB24_C3 == _InputImage.PixelFormat
                 || MVD_PIXEL_FORMAT.MVD_PIXEL_RGB_RGB24_P3 == _InputImage.PixelFormat)
                {
                    _InputImage.ConvertImagePixelFormat(MVD_PIXEL_FORMAT.MVD_PIXEL_MONO_08);
                }
                //// 显示控件读取图像
                mVDRender.LoadImageFromObject(_InputImage);
                mVDRender.Display();
                _ImageLoaded = true;
            }
        }

        /// <summary>
        /// 更新ROI按钮
        /// </summary>
        public void UpdateROIButtons()
        {
            // Clear existing controls
            ROIPanel.Children.Clear();

            int column = 0;

            foreach (var roiType in SupportedROIs)
            {
                var textBlock = new TextBlock
                {
                    Text = roiType.ToString(),
                    Height = 20,
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center,
                    Margin = new Thickness(12, 0, 0, 0)
                };
                Grid.SetColumn(textBlock, column);

                var radioButton = new RadioButton
                {
                    Name = $"{roiType}RadioButton",
                    Tag = roiType,
                    Width = 20,
                    Height = 20,
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center,
                    Margin = new Thickness(0, 0, 12, 0)
                };
                radioButton.Click += ROIButton_Click;
                Grid.SetColumn(radioButton, column + 1);

                ROIPanel.Children.Add(textBlock);
                ROIPanel.Children.Add(radioButton);

                column += 2; // Move to the next pair of columns
            }

            // Update Grid's column definitions dynamically
            ROIPanel.ColumnDefinitions.Clear();
            for (int i = 0; i < column; i++)
            {
                ROIPanel.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(i % 2 == 0 ? 2 : 1, GridUnitType.Star) });
            }
        }

        /// <summary>
        /// 更新掩膜ROI按钮
        /// </summary>
        public void UpdateMaskROIButtons()
        {
            // Clear existing controls
            MaskROIPanel.Children.Clear();

            int column = 0;

            foreach (var roiType in SupportedMaskROIs)
            {
                var textBlock = new TextBlock
                {
                    Text = roiType.ToString(),
                    Height = 20,
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center,
                    Margin = new Thickness(12, 0, 0, 0)
                };
                Grid.SetColumn(textBlock, column);

                var radioButton = new RadioButton
                {
                    Name = $"{roiType}RadioButton",
                    Tag = roiType,
                    Width = 20,
                    Height = 20,
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center,
                    Margin = new Thickness(0, 0, 12, 0)
                };
                radioButton.Click += MaskROIButton_Click;
                Grid.SetColumn(radioButton, column + 1);

                MaskROIPanel.Children.Add(textBlock);
                MaskROIPanel.Children.Add(radioButton);

                column += 2; // Move to the next pair of columns
            }

            // Update Grid's column definitions dynamically
            MaskROIPanel.ColumnDefinitions.Clear();
            for (int i = 0; i < column; i++)
            {
                MaskROIPanel.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(i % 2 == 0 ? 2 : 1, GridUnitType.Star) });
            }
        }

        /// <summary>
        /// 绘制圆弧
        /// </summary>
        private void OnDrawArcChanged()
        {
            _DrawArc.BorderColor = new MVD_COLOR(0, 255, 0, 255);
            AddShape(_DrawArc);
        }

        /// <summary>
        /// 绘制轮廓
        /// </summary>
        /// <param name="drawEdgePointList"></param>
        public void DrawOutline(List<CMvdLineSegmentF> LineSegmentFList)
        {
            foreach (var item in LineSegmentFList)
            {
                _DrawOutlineList.Add(item);
            }
            UpdateOutlinesVisibility();
        }

        /// <summary>
        /// 绘制矩形框图形（卡尺）
        /// </summary>
        /// <param name="caliperBoxList"></param>
        public void DrawRectangleFList(List<CMvdRectangleF> RectangleFList)
        {
            foreach (var item in RectangleFList)
            {
                _DrawRectangleFList.Add(item);
            }
            UpdateRectangleFVisibility();
        }

        /// <summary>
        /// 绘制基准点
        /// </summary>
        public void DrawPointSetF(CMvdPointSetF PointSetF)
        {
            this.ClearFixPoint();
            _drawFixPoint = PointSetF;
            UpdateFixPointVisibility();
        }

        /// <summary>
        /// 绘制点集（模板轮廓）
        /// </summary>
        public void DrawPointSetFList(List<CMvdPointSetF> PointSetFList)
        {
            foreach (var item in PointSetFList)
            {
                _MatchOutlineList.Add(item);
            }
            this.UpdateCPatMatchOutlineVisibility();
        }

        /// <summary>
        /// 清理所有结果
        /// </summary>
        public void Clear()
        {
            this.ClearArc();
            this.ClearCaliperBoxList();
            this.ClearOutline();
            this.ClearMatchOutlineList();
            this.ClearFixPoint();
        }

        /// <summary>
        /// 清理基准点
        /// </summary>
        public void ClearFixPoint()
        {
            RemoveShape(_drawFixPoint);
            _drawFixPoint = null;
        }

        /// <summary>
        /// 清理圆弧
        /// </summary>
        public void ClearArc()
        {
            if (null != _DrawArc)
            {
                RemoveShape(_DrawArc);
                _DrawArc = null;
            }
        }

        /// <summary>
        /// 清理卡尺
        /// </summary>
        public void ClearCaliperBoxList()
        {
            foreach (var item in _DrawRectangleFList)
            {
                RemoveShape(item);
            }
            _DrawRectangleFList.Clear();
        }

        /// <summary>
        /// 清理轮廓
        /// </summary>
        public void ClearOutline()
        {
            foreach (var item in _DrawOutlineList)
            {
                RemoveShape(item);
            }
            _DrawOutlineList.Clear();
        }

        /// <summary>
        /// 清理匹配轮廓
        /// </summary>
        public void ClearMatchOutlineList()
        {
            foreach (var item in _MatchOutlineList)
            {
                RemoveShape(item);
            }
            _MatchOutlineList.Clear();
        }

        /// <summary>
        /// 添加图形
        /// </summary>
        /// <param name="shape"></param>
        public void AddShape(CMvdShape shape)
        {
            if (!_addedShapes.Contains(shape))
            {
                mVDRender.AddShape(shape);
                _addedShapes.Add(shape);
            }
        }

        /// <summary>
        /// 删除图形
        /// </summary>
        /// <param name="shape"></param>
        public void RemoveShape(CMvdShape shape)
        {
            if (_addedShapes.Contains(shape))
            {
                mVDRender.DeleteShape(shape);
                _addedShapes.Remove(shape);
            }
        }

        // 更新圆弧显示状态
        private void UpdateArcVisibility()
        {
            if (_DrawArc != null)
            {
                if (_isArcVisible)
                {
                    AddShape(_DrawArc);
                }
                else
                {
                    RemoveShape(_DrawArc);
                }
                mVDRender.Display();
            }
        }

        // 更新卡尺显示状态
        private void UpdateRectangleFVisibility()
        {
            foreach (var item in _DrawRectangleFList)
            {
                if (_isRectangleFVisible)
                {
                    AddShape(item);
                }
                else
                {
                    RemoveShape(item);
                }
            }
            mVDRender.Display();
        }

        // 更新轮廓显示状态
        private void UpdateOutlinesVisibility()
        {
            foreach (var outline in _DrawOutlineList)
            {
                if (_areOutlinesVisible)
                {
                    AddShape(outline);
                }
                else
                {
                    RemoveShape(outline);
                }
            }
            mVDRender.Display();
        }

        /// <summary>
        /// 更新模板轮廓
        /// </summary>
        private void UpdateCPatMatchOutlineVisibility()
        {
            foreach (var outline in _MatchOutlineList)
            {
                if (_areMatchOutlineVisible)
                {
                    AddShape(outline);
                }
                else
                {
                    RemoveShape(outline);
                }
            }
            mVDRender.Display();
        }

        /// <summary>
        /// 更新基准点
        /// </summary>
        private void UpdateFixPointVisibility()
        {
            if (_isFixPointVisible)
            {
                AddShape(_drawFixPoint);
            }
            else
            {
                RemoveShape(_drawFixPoint);
            }
            mVDRender.Display();
        }

        #endregion

        #region Action

        private void Button_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(ButtonViewModel.IsChecked))
            {
                var buttonViewModel = sender as ButtonViewModel;

                // 根据按钮的状态更新视图模型的属性
                switch (buttonViewModel.Name)
                {
                    case "圆弧":
                        _isArcVisible = buttonViewModel.IsChecked;
                        UpdateArcVisibility();
                        break;
                    case "卡尺":
                        _isRectangleFVisible = buttonViewModel.IsChecked;
                        UpdateRectangleFVisibility();
                        break;
                    case "轮廓":
                        _areOutlinesVisible = buttonViewModel.IsChecked;
                        UpdateOutlinesVisibility();
                        break;
                    case "匹配框":
                        _isRectangleFVisible = buttonViewModel.IsChecked;
                        UpdateRectangleFVisibility();
                        break;
                    case "匹配轮廓":
                        _areMatchOutlineVisible = buttonViewModel.IsChecked;
                        UpdateCPatMatchOutlineVisibility();
                        break;
                    case "基准点":
                        _isFixPointVisible = buttonViewModel.IsChecked;
                        UpdateFixPointVisibility();
                        break;
                        // 添加其他按钮的逻辑...
                }
            }
        }

        private void ROIButton_Click(object sender, RoutedEventArgs e)
        {
            var button = sender as RadioButton;
            if (button != null && button.Tag is ROIType roiType)
            {
                SelectedROI = roiType;
            }

            if (SelectedROI is ROIType.全图)
            {
                try
                {
                    switch (_roiDataType)
                    {
                        case ROIDataTypeEnum.RegionList:
                            {
                                // 删除Region
                                foreach (var item in _drawRegionList)
                                {
                                    mVDRender.DeleteShape(item.Shape);
                                }
                                _drawRegionList.Clear();
                                _drawFixPoint = null;
                                mVDRender.Display();

                                break;
                            }
                        case ROIDataTypeEnum.ROI:
                            {
                                bool isShapeDeleted = false;
                                if (null != _roi)
                                {
                                    mVDRender.DeleteShape(_roi);
                                    _roi = null;
                                    isShapeDeleted = true;
                                }
                                foreach (var item in _MaskShapeList)
                                {
                                    mVDRender.DeleteShape(item);
                                    isShapeDeleted = true;
                                }
                                _MaskShapeList.Clear();

                                if (isShapeDeleted)
                                {
                                    mVDRender.Display();
                                    Logger.Log("Existing ROI has been cleared.\r\n");
                                }
                                break;
                            }
                        default:
                            throw new InvalidOperationException("Unknown active data type.");
                    }
                }
                catch (MvdException ex)
                {
                    Logger.Log(String.Format("Fail to clear existing shapes, Module : {0}, ErrorCode : 0x{1}, Message : {2}.\r\n", ex.ModuleType.ToString(), ex.ErrorCode.ToString("X"), ex.Message));
                }
                catch (System.Exception ex)
                {
                    Logger.Log(String.Format("Fail to clear existing shapes, Message : {0}, StackTrace : {1}.\r\n", ex.Message, ex.StackTrace));
                }
            }
        }

        private void MaskROIButton_Click(object sender, RoutedEventArgs e)
        {
            var button = sender as RadioButton;
            if (button != null && button.Tag is ROIType roiType)
            {
                SelectedMaskROI = roiType;
            }
        }

        /// <summary>
        /// 触发事件
        /// </summary>
        /// <param name="enEventType"></param>
        /// <param name="enShapeType"></param>
        /// <param name="cShapeObj"></param>
        private void MVDRender_MVDShapeChangedEvent(MVDRenderActivex.MVD_SHAPE_EVENT_TYPE enEventType, MVD_SHAPE_TYPE enShapeType, CMvdShape cShapeObj)
        {
            if (MVDRenderActivex.MVD_SHAPE_EVENT_TYPE.MVD_SHAPE_SELECTED == enEventType)
            {
                return;
            }
            if (MVDRenderActivex.MVD_SHAPE_EVENT_TYPE.MVD_SHAPE_EDITED == enEventType)
            {
                return;
            }

            /* 图形删除事件 */
            if (MVDRenderActivex.MVD_SHAPE_EVENT_TYPE.MVD_SHAPE_DELETED == enEventType)
            {
                // ROI删除事件
                switch (_roiDataType)
                {
                    case ROIDataTypeEnum.RegionList:
                        {
                            // 删除Region
                            foreach (var item in _drawRegionList)
                            {
                                if (cShapeObj == item.Shape)
                                {
                                    _drawRegionList.Remove(item);
                                    break;
                                }
                            }
                            if (cShapeObj == _drawFixPoint)
                            {
                                _drawFixPoint = null;
                            }
                            break;
                        }
                    case ROIDataTypeEnum.ROI:
                        {
                            // 检查掩膜列表中是否包含该形状，如果包含则删除
                            if (_MaskShapeList.Contains(cShapeObj))
                            {
                                _MaskShapeList.Remove(cShapeObj);
                            }
                            if (_roi == cShapeObj)
                            {
                                _roi = null;
                            }
                            break;
                        }
                    default:
                        throw new InvalidOperationException("Unknown active data type.");
                }
                return;
            }

            /* 选择全图不允许绘制图形 */
            if (SelectedROI is ROIType.全图)
            {
                MessageBox.Show("不允许在AllRegion模式下添加形状");
                mVDRender.DeleteShape(cShapeObj);
                mVDRender.Display();
                return;
            }

            /* 图形增加事件 */
            if (MVDRenderActivex.MVD_SHAPE_EVENT_TYPE.MVD_SHAPE_ADDED == enEventType)
            {
                switch (_roiDataType)
                {
                    case ROIDataTypeEnum.RegionList:
                        {
                            var region = new CPatMatchRegion();
                            switch (cShapeObj.ShapeType)
                            {
                                // 矩形
                                case MVD_SHAPE_TYPE.MvdShapeRectangle:
                                    {
                                        region.Shape = cShapeObj as CMvdRectangleF;
                                    }
                                    break;
                                // 扇环
                                case MVD_SHAPE_TYPE.MvdShapeAnnularSector:
                                    {
                                        region.Shape = cShapeObj as CMvdAnnularSectorF;
                                    }
                                    break;
                                // 多边形
                                case MVD_SHAPE_TYPE.MvdShapePolygon:
                                    {
                                        region.Shape = cShapeObj as CMvdPolygonF;
                                    }
                                    break;
                                case MVD_SHAPE_TYPE.MvdShapeCircle:
                                    {
                                        region.Shape = cShapeObj as CMvdCircleF;
                                    }
                                    break;
                                default:
                                    {
                                        MessageBox.Show("The shape is not supported.\r\n");
                                        break;
                                    }
                            }

                            if (((SelectedROI is ROIType.矩形) && (MVD_SHAPE_TYPE.MvdShapeRectangle == enShapeType))
                               || ((SelectedROI is ROIType.扇环) && (MVD_SHAPE_TYPE.MvdShapeAnnularSector == enShapeType))
                               || ((SelectedROI is ROIType.多边形) && (MVD_SHAPE_TYPE.MvdShapePolygon == enShapeType))
                               || ((SelectedROI is ROIType.圆) && (MVD_SHAPE_TYPE.MvdShapeCircle == enShapeType)))
                            {
                                region.Sign = true;
                                _drawRegionList.Add(region);
                            }
                            else if (((SelectedMaskROI is ROIType.矩形) && (MVD_SHAPE_TYPE.MvdShapeRectangle == enShapeType))
                               || ((SelectedMaskROI is ROIType.扇环) && (MVD_SHAPE_TYPE.MvdShapeAnnularSector == enShapeType))
                               || ((SelectedMaskROI is ROIType.多边形) && (MVD_SHAPE_TYPE.MvdShapePolygon == enShapeType)))
                            {
                                region.Sign = false;
                                _drawRegionList.Add(region);
                            }
                            else
                            {
                                MessageBox.Show("The shape that is inconsistent with the checked type will be deleted.\r\n");
                                mVDRender.DeleteShape(cShapeObj);
                                mVDRender.Display();
                            }
                            break;
                        }
                    case ROIDataTypeEnum.ROI:
                        {
                            // 处理ROI
                            if (((SelectedROI is ROIType.矩形) && (MVD_SHAPE_TYPE.MvdShapeRectangle == enShapeType))
                                || ((SelectedROI is ROIType.圆) && (MVD_SHAPE_TYPE.MvdShapeCircle == enShapeType))
                                || ((SelectedROI is ROIType.扇环) && (MVD_SHAPE_TYPE.MvdShapeAnnularSector == enShapeType))
                                || ((SelectedROI is ROIType.多边形) && (MVD_SHAPE_TYPE.MvdShapePolygon == enShapeType))
                                || ((SelectedROI is ROIType.平行四边形) && (MVD_SHAPE_TYPE.MvdShapeParallelogram == enShapeType))
                                || ((SelectedROI is ROIType.线段) && (MVD_SHAPE_TYPE.MvdShapeLineSegment == enShapeType))
                                || ((SelectedROI is ROIType.折线段) && (MVD_SHAPE_TYPE.MvdShapePolylineSegment == enShapeType)))
                            {
                                if (_roi != null)
                                {
                                    mVDRender.DeleteShape(_roi);
                                    mVDRender.Display();
                                }
                                _roi = cShapeObj;
                            }
                            else if (((SelectedMaskROI is ROIType.矩形) && (MVD_SHAPE_TYPE.MvdShapeRectangle == enShapeType))
                                || ((SelectedMaskROI is ROIType.圆) && (MVD_SHAPE_TYPE.MvdShapeCircle == enShapeType))
                                || ((SelectedMaskROI is ROIType.扇环) && (MVD_SHAPE_TYPE.MvdShapeAnnularSector == enShapeType))
                                || ((SelectedMaskROI is ROIType.多边形) && (MVD_SHAPE_TYPE.MvdShapePolygon == enShapeType))
                                || ((SelectedMaskROI is ROIType.平行四边形) && (MVD_SHAPE_TYPE.MvdShapeParallelogram == enShapeType))
                                || ((SelectedMaskROI is ROIType.线段) && (MVD_SHAPE_TYPE.MvdShapeLineSegment == enShapeType))
                                || ((SelectedMaskROI is ROIType.折线段) && (MVD_SHAPE_TYPE.MvdShapePolylineSegment == enShapeType)))
                            {
                                _MaskShapeList.Add(cShapeObj);
                            }
                            else
                            {
                                MessageBox.Show("The shape that is inconsistent with the checked type will be deleted.\r\n");
                                mVDRender.DeleteShape(cShapeObj);
                                mVDRender.Display();
                            }
                            break;
                        }
                    default:
                        throw new InvalidOperationException("Unknown active data type.");
                }
            }
        }

        private void MVDRender_MVDMouseEvent(MVDMouseEventType enMouseEventType, int nPointX, int nPointY, short nZDelta)
        {
            //用户想要实现自定义交互需通过SetConfiguration接口启用自定义交互
            //用户可根据enMouseEventType判断鼠标事件类型，编写对应的响应函数
            //示例：实时显示鼠标所在位置的图像坐标和像素值
            try
            {
                //窗口坐标转图像坐标
                float fImgX = 0.0f, fImgY = 0.0f;
                mVDRender.TransformCoordinate(nPointX, nPointY, ref fImgX, ref fImgY, MVDCoordTransType.Wnd2Img);

                //获取像素信息显示
                do
                {
                    if (false == _ImageLoaded)
                    {
                        break;
                    }

                    int nImagePointX = (int)fImgX;
                    int nImagePointY = (int)fImgY;
                    int nWidth = (int)_InputImage.Width;
                    int nHeight = (int)_InputImage.Height;
                    if (nImagePointX < 0 || nImagePointX >= nWidth
                        || nImagePointY < 0 || nImagePointY >= nHeight)
                    {
                        break;
                    }

                    string pixelInfo = string.Empty;
                    List<byte> pixelValue = _InputImage.GetPixel(nImagePointX, nImagePointY);
                    MVD_PIXEL_FORMAT enPixelFormat = _InputImage.PixelFormat;
                    if (MVD_PIXEL_FORMAT.MVD_PIXEL_MONO_08 == enPixelFormat)
                    {
                        pixelInfo = string.Format("X:{0:D4} Y:{1:D4} | R:{2:D3} G:{3:D3} B:{4:D3}", nImagePointX, nImagePointY, pixelValue[0], pixelValue[0], pixelValue[0]);
                    }
                    else if (MVD_PIXEL_FORMAT.MVD_PIXEL_RGB_RGB24_C3 == enPixelFormat)
                    {
                        pixelInfo = string.Format("X:{0:D4} Y:{1:D4} | R:{2:D3} G:{3:D3} B:{4:D3}", nImagePointX, nImagePointY, pixelValue[0], pixelValue[1], pixelValue[2]);
                    }
                    else
                    {
                        throw new MvdException(MVD_MODULE_TYPE.MVD_MODUL_APP, MVD_ERROR_CODE.MVD_E_SUPPORT, "Unsupported pixel format.");
                    }
                    //this.tbPixelInfo.Text = pixelInfo;
                } while (false);
            }
            catch (MvdException ex)
            {
                Logger.Log(String.Format("Fail to respond to mouse event! Module : {0}, ErrorCode : 0x{1}, Message : {2}.\r\n", ex.ModuleType.ToString(), ex.ErrorCode.ToString("X"), ex.Message));
            }
            catch (System.Exception ex)
            {
                Logger.Log(String.Format("Fail to respond to mouse event! Message : {0}, StackTrace : {1}.\r\n", ex.Message, ex.StackTrace));
            }
        }

        #endregion

    }

    /// <summary>
    /// 支持的ROI类型
    /// </summary>
    public enum ROIType
    {
        全图,
        矩形,
        圆,
        扇环,
        多边形,
        线段,
        折线段,
        平行四边形
    }


    public class ButtonViewModel : ObservableObject
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set { SetProperty(ref _name, value); }
        }

        private bool _isChecked;
        public bool IsChecked
        {
            get { return _isChecked; }
            set { SetProperty(ref _isChecked, value); }
        }
    }

}
